﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;

using ERPStore.Extensions;
using CloudSoft.Workflows;

using CloudSoft.Extensions;

namespace ERPStore.Exporter.Workflows.Activities
{

	public sealed class ApplyProductFilter : CodeActivity
	{
		public InArgument<List<ERPStore.Models.Product>> ProductList { get; set; }
		public InArgument<List<ERPStore.Models.ProductStockInfo>> ProductStockList { get; set; }
		public InArgument<Models.Settings> Settings { get; set; }
		public InArgument<List<int>> BanishedProductList { get; set; }
		public OutArgument<List<ERPStore.Models.Product>> FilteredProductListArgument { get; set; }

		protected override void Execute(CodeActivityContext context)
		{
			var productList = context.GetValue(this.ProductList);
			var productStockList = context.GetValue(this.ProductStockList);
			var settings = context.GetValue(this.Settings);
			var banishedProductList = context.GetValue(this.BanishedProductList);
			var loggerService = context.GetService<ERPStore.Logging.ILogger>();

			var filteredProductList = new List<ERPStore.Models.Product>();

			// Copy
			foreach (var item in productList)
			{
				filteredProductList.Add(item);
			}

			// on sait jamais
			filteredProductList.RemoveAll(i => i == null);

			var count = productList.Count;

			if (banishedProductList != null)
			{
				filteredProductList.RemoveAll(i => banishedProductList.Contains(i.Id));
			}

			if (!settings.AllowMissingDefaultImage)
			{
				filteredProductList.RemoveAll(i => i.DefaultImage == null);
			}
			if (!settings.AllowNotSellable)
			{
				filteredProductList.RemoveAll(i => !i.IsSellable);
			}
			if (!settings.AllowNullLongDescription)
			{
				filteredProductList.RemoveAll(i => i.LongDescription.IsNullOrTrimmedEmpty());
			}
			if (!settings.AllowNullShortDescription)
			{
				filteredProductList.RemoveAll(i => i.ShortDescription.IsNullOrTrimmedEmpty());
			}
			if (!settings.AllowNullStrengthsPoints)
			{
				filteredProductList.RemoveAll(i => i.StrengthsPoints.IsNullOrTrimmedEmpty());
			}
			if (!settings.AllowVariation)
			{
				filteredProductList.RemoveAll(i => i.IsParentVariation);
			}
			if (!settings.AllowVariant)
			{
				filteredProductList.RemoveAll(i => i.VariationProductParentId.HasValue);
			}
			if (!settings.AllowZeroSalePrice)
			{
				filteredProductList.RemoveAll(i => i.BestPrice.Value == 0);
			}
			if (!settings.AllowStockEmpty)
			{
				filteredProductList.RemoveAll(i => productStockList.FirstOrDefault(s => s.ProductCode == i.Code) == null
											|| productStockList.FirstOrDefault(s => s.ProductCode == i.Code).AvailableStock == 0);
			}
			if (settings.BarcodeMandatory)
			{
				filteredProductList.RemoveAll(i => i.Gtin.EAN.IsNullOrTrimmedEmpty()
										&& i.Gtin.UPC.IsNullOrTrimmedEmpty()
										&& i.Gtin.ISBN.IsNullOrTrimmedEmpty());
			}

			if (settings.RestrictToOrdered)
			{
				filteredProductList.RemoveAll(i => i.Stats.OrderCount.GetValueOrDefault(0) > 0);
			}

			if (settings.MinimalSalePrice.HasValue)
			{
				filteredProductList.RemoveAll(i => (i.BestPrice.Value * i.MinimumSaleQuantity) <= settings.MinimalSalePrice);
			}

			if (settings.MaximumSalePrice.HasValue)
			{
				filteredProductList.RemoveAll(i => (i.BestPrice.Value * i.MinimumSaleQuantity) >= settings.MaximumSalePrice);
			}

			context.SetValue(this.FilteredProductListArgument, filteredProductList);
			var removedCount = count - filteredProductList.Count();
			if (removedCount > 0)
			{
				var message = string.Format("{0} products removed by filter for export", removedCount);
				context.ReportProgress(message);
				loggerService.Debug(message);
			}
		}
	}
}
